package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 考试吧
 * @author Persh
 *
 */
public class Exam8NewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.exam8.com/.*/[0-9]{6}/[0-9]*.html"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h1.c036").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("div.title").text();
			if(StringUtils.isBlank(title)){
				title =doc.select("h1.nrtext").text();
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.tskpinpjnc"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.rcuxsgsfbf"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.deudaufyrm"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div.qsignxoavj"));
					if(StringUtils.isBlank(content)){
						content=HtmlCleaner.getContentHtml(url,doc.select("div.ugyfehcoyj"));
						if (StringUtils.isBlank(content)) {
							content=HtmlCleaner.getContentHtml(url,doc.select("div.featjbvivf"));
							if (StringUtils.isBlank(content)) {
								content=HtmlCleaner.getContentHtml(url,doc.select("div.hpxnprvoks"));
								if(StringUtils.isBlank(content)){
									content=HtmlCleaner.getContentHtml(url,doc.select("div.left"));
									content=StringUtils.substringAfter(content, "】");
								}

							}
						}
					}
				}
			}
		}
		date=DateUtils.matchDate(doc.select("div.xytitle01").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.titiefu").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("div.nrlaiyuan").text());
				if(date==null){
					date=DateUtils.matchDate(doc.select("div.title01").text());
				}
			}
		}
		String author=null;
		author=doc.select("div.titiefu a").text();
		author=StringUtils.substringBefore(author, " ");
		news.setAuthor(author);
		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		Exam8NewsAnalyse analyse=new Exam8NewsAnalyse();
		String url="http://www.exam8.com/kuaiji/ACCA/hangye/201405/2904739.html";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
